/**
 * 将一个层次的数据结构，转换为平行的父子数据结构
 * @param nodesArray
 * @returns {*}
 */
function array2Nodes(nodesArray) {
  var i,
    l,
    key = "id",
    parentKey = "pId",
    childKey = "sub_button";
  if (!key || key == "" || !nodesArray) return [];

  if (nodesArray instanceof Array) {
    var r = [];
    var tmpMap = {};
    for (i = 0, l = nodesArray.length; i < l; i++) {
      tmpMap[nodesArray[i][key]] = nodesArray[i];
    }
    for (i = 0, l = nodesArray.length; i < l; i++) {
      if (
        tmpMap[nodesArray[i][parentKey]] &&
        nodesArray[i][key] != nodesArray[i][parentKey]
      ) {
        if (!tmpMap[nodesArray[i][parentKey]][childKey])
          tmpMap[nodesArray[i][parentKey]][childKey] = [];
        tmpMap[nodesArray[i][parentKey]][childKey].push(nodesArray[i]);
      } else {
        r.push(nodesArray[i]);
      }
    }
    return r;
  } else {
    return [nodesArray];
  }
}

var menuArray = [
  {
    id: "AE9CB92989FFB1523D8C63C3124AB372F074F8898BE7B930FABCE06BF60CB237",
    pId: "-1",
    key: "4C0F11D31571F75A00F620C82E5847E89837FDD758C5027E4D1DDFE959697F85",
    url: "http://",
    media_id: "null",
    name: "菜单名称",
    type: "view",
    orderId: 1,
    msgText: "null",
  },
  {
    id: "FD5C1B24A580DB7DE7F472736B7A06277FE8C17BB8739F9D3BE2B19D866C8470",
    pId: "-1",
    key: "909CE8ADA256BEEE2E37A78C0CDD823B273B04C60F797A537EC174A0377D7B8C",
    url: "null",
    media_id: "null",
    name: "菜单名称",
    type: "click",
    orderId: 2,
    msgText: "null",
  },
  {
    id: "324703D728232820DE9A8305D183B3289AE708DBBF4B8F4D044F7BC9714B7F80",
    pId: "FD5C1B24A580DB7DE7F472736B7A06277FE8C17BB8739F9D3BE2B19D866C8470",
    key: "C9B73D24A5EAD91772CFEC5BD129B7EB2C2A9A96E3EF743BAF54B53E29758837",
    url: "null",
    media_id: "null",
    name: "菜单名称",
    type: "click",
    orderId: 1,
    msgText: "null",
  },
  {
    id: "00DB54295F5A87248BEE3B7160C217066A6CE77C00B9EC2A415CE15898390425",
    pId: "AE9CB92989FFB1523D8C63C3124AB372F074F8898BE7B930FABCE06BF60CB237",
    key: "05810B96BBDC28AAB99EBC69E70F9196C42434A042C0468F6A8DDB0875D22972",
    url: "null",
    media_id: "null",
    name: "菜单名称",
    type: "click",
    orderId: 1,
    msgText: "null",
  },
  {
    id: "547B7E126D694B4DC9B831951B3F5A2A378ED4AA0DA9955C088A076F865BC1B1",
    pId: "AE9CB92989FFB1523D8C63C3124AB372F074F8898BE7B930FABCE06BF60CB237",
    key: "4EA0242BE5E66E90FB7931B804472DE68CE17D46817CD699F38E21903B1DEAE6",
    url: "null",
    media_id: "null",
    name: "菜单名称",
    type: "click",
    orderId: 2,
    msgText: "null",
  },
];

var nodes = array2Nodes(menuArray);
console.log(array2Nodes(nodes));

/**
 * 将平行父子数据结构转为层次结构
 * @param nodes
 * @returns {Array}
 */
function nodes2Array(nodes) {
  if (!nodes) return [];
  var childKey = "childs",
    r = [];
  if (nodes instanceof Array) {
    for (var i = 0, l = nodes.length; i < l; i++) {
      r.push(nodes[i]);
      if (nodes[i][childKey]) r = r.concat(nodes2Array(nodes[i][childKey]));
    }
  } else {
    r.push(nodes);
    if (nodes[childKey]) r = r.concat(nodes2Array(nodes[childKey]));
  }
  return r;
}
const testNodes = [
  {
    childs: [
      {
        id: 2,
        labelId: 2,
        mediaType: 3,
        name: 4,
        parentId: 5,
        regionId: 6,
        childs: [
          {
            id: 3,
            labelId: 2,
            mediaType: 3,
            name: 4,
            parentId: 5,
            regionId: 6,
            childs: [],
          },
        ],
      },
    ],
    id: 1,
    labelId: 2,
    mediaType: 3,
    name: 4,
    parentId: 5,
    regionId: 6,
  },
];

console.log(nodes2Array(testNodes));
